const baseUrl = 'https://restapi.amap.com/v3/'
const key = '0a379910459ad922ace4eee0bf3fa105'
/**
 * Direction
 * @param {*} params origin: '116.434307,39.90909', destination: '116.434446,39.90816'
 * @returns promise
 */
const DirectionWalk = (params = { origin: '116.434307,39.90909', destination: '116.434446,39.90816' }) => {
  return new Promise((resolve, reject) => {
    const modelUrl = baseUrl + 'direction/walking'
    var url = new URL(modelUrl)
    url.searchParams.append('key', key)
    for (const k in params) {
      url.searchParams.append(k, params[k])
    }
    fetch(url)
      .then((response) => {
        if (!response.ok) {
          throw new Error('Network response was not ok')
        }
        return response.json()
      })
      .then((data) => resolve(data))
      .catch((error) => reject(error))
  })
}
export { DirectionWalk }
